package com.freedom.clue.controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import com.freedom.auth.vo.ClueExportVo;
import com.freedom.clue.service.IClueService;
import com.freedom.clue.domain.Clue;
import com.freedom.clue.query.ClueQuery;
import com.freedom.base.util.AjaxResult;
import com.freedom.base.util.PageInfo;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.FileOutputStream;
import java.io.Serializable;
import java.util.List;

@RestController
@RequestMapping("/clue")
public class ClueController {
    @Autowired
    public IClueService clueService;


    /**
     * 保存和修改公用的
     *
     * @param clue 传递的实体
     * @return Ajaxresult转换结果
     */
    @PostMapping
    public AjaxResult insertOrUpdate ( @RequestBody Clue clue ) {
        try {
            if (clue.getId() != null) {
                clueService.update(clue);
                return AjaxResult.success("修改成功");
            } else {
                clueService.insert(clue);
                return AjaxResult.success("新增成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("保存失败！" + e.getMessage());
        }
    }

    /**
     * 删除对象信息
     *
     * @param id
     * @return
     */
    @DeleteMapping(value = "/{id}")
    public AjaxResult delete ( @PathVariable("id") Serializable id ) {
        try {
            clueService.deleteById(id);
            return AjaxResult.success("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("删除失败！" + e.getMessage());
        }
    }

    //获取用户
    @GetMapping("/{id}")
    public AjaxResult getById ( @PathVariable("id") Serializable id ) {
        try {
            Clue clue = clueService.getById(id);
            return AjaxResult.success(clue, "查询成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("查询失败！" + e.getMessage());
        }
    }


    /**
     * 查看所有的员工信息
     *
     * @return
     */
    @GetMapping
    public AjaxResult getAll () {
        try {
            List<Clue> list = clueService.getAll();
            return AjaxResult.success(list, "查询成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("查询失败！" + e.getMessage());
        }
    }


    /**
     * 分页查询数据
     *
     * @param query 查询对象
     * @return PageList 分页对象
     */
    @PutMapping
    public AjaxResult queryPage ( @RequestBody ClueQuery query ) {
        try {
            PageInfo<Clue> pageInfo = clueService.queryPage(query);
            return AjaxResult.success(pageInfo, "分页查询成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("分页查询失败！" + e.getMessage());
        }
    }


    /**
     * 批量删除
     */
    @PatchMapping
    public AjaxResult batchDelete ( @RequestBody List<Long> ids ) {
        try {
            clueService.batchDelete(ids);
            return AjaxResult.success("批量删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("批量删除失败");
        }
    }

    @PostMapping("/importExcel")
    public AjaxResult importExcel ( @RequestParam("file") MultipartFile file ) {
        try {
            String result = clueService.importExcel(file);
            return AjaxResult.success(result, "导入成功");
        } catch (Exception e) {
            return AjaxResult.error("导入失败");
        }
    }

    @PostMapping("/exportExcel")
    public AjaxResult exportExcel ( String title ) {
        try {
            List<ClueExportVo> list = clueService.getClueExportList();

            ExportParams params = new ExportParams(title, "sheet", ExcelType.XSSF);

            Workbook workbook = ExcelExportUtil.exportExcel(params, ClueExportVo.class, list);

//            File file = new File("excel");
//            if (!file.exists()) {
//                file.mkdirs();
//            }

            FileOutputStream fos = new FileOutputStream("C:\\Users\\freedom\\Desktop\\1.xlsx");

            workbook.write(fos);
            fos.close();

            return AjaxResult.success("导出成功");
        } catch (Exception e) {
            return AjaxResult.error("导出失败");
        }
    }

}
